package org.hncors.gnssparse.activity;

import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.southgnss.connect.DeviceParManage;
import com.southgnss.connect.TopDeviceManage;
import com.southgnss.gnssparse.GnssSateInfo;
import com.southgnss.gnssparse.GnssSateStatus;
import com.southgnss.gnssparse.GnssSateSysType;
import com.southgnss.location.DateUtil;

import org.hncors.gnssparse.R;
import org.hncors.gnssparse.adapter.LocationItemAdapter;
import org.hncors.gnssparse.adapter.MsgListAdapter;
import org.hncors.gnssparse.adapter.SatelliteItemAdapter;
import org.hncors.gnssparse.base.BaseActivity;
import org.hncors.gnssparse.base.MyApplication;
import org.hncors.gnssparse.fragement.FileLogger;
import org.hncors.gnssparse.fragement.GnssListFragment;
import org.hncors.gnssparse.model.LocationItemData;
import org.hncors.gnssparse.model.Message;
import org.hncors.gnssparse.model.SatelliteItemData;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


public class LocationListActivity extends BaseActivity {
    private static final String TAG = "LocationListActivity";



    private LocationItemAdapter mSatelliteItemAdapter = null;

    private FileLogger mFileLogger;
    static Handler mHandler = new Handler();
    Runnable onTimerRunnable = new Runnable() {
        @Override
        public void run() {
            TopDeviceManage extras = TopDeviceManage.getInstance(null);
            String strInfo = String.format(Locale.ENGLISH, "%s:%.10f%n %s:%.10f%n %s:%.4f%n %s:%.4f%n %s:%.4f%n " +
                            "%s:%.4f%n %s:%.4f%n %s:%.4f%n %s:%.4f%n %s:%.4f%n %s:%s%n %s:%s%n %s:%d%n %s:%d%n " +
                            "%s:%s%n %s:%s%n %s:%s%n %s:%s%n "

                    , getString(R.string.B), TopDeviceManage.getInstance(null).getLatitude()
                    , getString(R.string.L), TopDeviceManage.getInstance(null).getLongitude()
                    , getString(R.string.H), TopDeviceManage.getInstance(null).getAltitude()
                    , getString(R.string.HRMS), TopDeviceManage.getInstance(null).getHrms()
                    , getString(R.string.speed), TopDeviceManage.getInstance(null).getSpeed()
                    , getString(R.string.heading), TopDeviceManage.getInstance(null).getBearing()
                    , getString(R.string.PDOP), TopDeviceManage.getInstance(null).getPdop()
                    , getString(R.string.HDOP), TopDeviceManage.getInstance(null).getHdop()
                    , getString(R.string.VDOP), TopDeviceManage.getInstance(null).getVdop()
                    , getString(R.string.VRMS), TopDeviceManage.getInstance(null).getVrms()
                    , getString(R.string.statusType), getCurSolutionState(TopDeviceManage.getInstance(null).getStatusType())
                    , getString(R.string.time), DateUtil.formatDate(new Date(TopDeviceManage.getInstance(null).getLocalTime()))
                    , getString(R.string.age), TopDeviceManage.getInstance(null).getAge()
                    , getString(R.string.m_tSatNum), TopDeviceManage.getInstance(null).getSatInView()
                    , "固件版本", DeviceParManage.GetInstance().GetRTKAppVer()
                    , "工作模式", DeviceParManage.GetInstance().GetCurSysMode()
                    , "电池剩余", DeviceParManage.GetInstance().getPowerFree().isEmpty() ? "" : (DeviceParManage.GetInstance().getPowerFree() + "%")
                    , "差分站ID", TopDeviceManage.getInstance(null).getChangeStationID()
            );
//           // tvGnssInfo.setText(strInfo);
//            LocationItemData item = new LocationItemData();
//            // 卫星列表
//            item.bValue = extras.getLatitude() + "";
//            item.lValue = extras.getLongitude() + "";
//            item.hValue = extras.getAltitude() + "";
//            item.timeStr = DateUtil.formatDate(new Date(extras.getLocalTime()));
//            item.mSatelliteStatus = getCurSolutionState(extras.getStatusType());
//            if (extras.getLatitude() > 0 && extras.getLongitude() > 0) {
//
//
//            }
            mSatelliteItemAdapter.notifyDataSetChanged();
            mFileLogger.onNmeaReceived(System.currentTimeMillis(), strInfo);
            mHandler.postDelayed(onTimerRunnable, 1000);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        appendMainBody(this, R.layout.layout_gnss_history_satellite_list);
        appendTopBody(R.layout.activity_top_text);
        setTopBarTitle("历史记录");
        topRightText.setText("下载");
        mFileLogger = new FileLogger(this);
        topRightText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mFileLogger.send();
            }
        });
        initView();
        mFileLogger.startNewLog();
        mHandler.post(onTimerRunnable);
    }
    //    控件初始化控件
    //    控件初始化控件
    private void initView() {
        ListView satelliteListView = (ListView) findViewById(R.id.listViewSatelliteList);
        if (satelliteListView != null) {
            mSatelliteItemAdapter = new LocationItemAdapter(this, MyApplication.getInstance().mSateItemsDataArray);
            satelliteListView.setAdapter(mSatelliteItemAdapter);
        }
    }
    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private String getCurSolutionState(int statusType) {
        String statusTypeString = "";
        switch (statusType) {
            case 0:// 无效定位
                break;
            case 1:// 单点
                statusTypeString = "单点";
                break;
            case 2:// 差分
            case 3:
                statusTypeString = "差分";
                break;
            case 4:// 固定
                statusTypeString = "固定";
                break;
            case 8:
                statusTypeString = "固定";
                break;
            case 5:// 浮动
                statusTypeString = "浮动";
                break;
            default:
                break;
        }
        return statusTypeString;
    }
    public void setFileLogger(FileLogger value) {
        mFileLogger = value;
    }

    public class MyComparator implements Comparator<GnssSateInfo> {

        public int compare(GnssSateInfo s1, GnssSateInfo s2) {
            if(s1.getPrn() > s2.getPrn()){
                return 1;
            } else if(s1.getPrn() < s2.getPrn()) {
                return -1;
            }
            return 0;
        }
    }
    }
